package com.koala.mylittlesecretary;

import android.content.Context;
import android.media.AudioManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MyPhoneStateListener extends PhoneStateListener {

	Context myContext;

	public static Boolean phoneRinging = false;
	ContactItems myCI;
	Settings mySettings;

	AudioManager myAM;
	TelephonyManager myTM;

	@Override
	public void onCallStateChanged(int state, String incomingNumber) {
		if (mySettings.bActive) {
			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE:
				// Log.d("MLS", "IDLE");
				myAM.setRingerMode(AudioManager.RINGER_MODE_SILENT);
				phoneRinging = false;
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:
				Log.d("MLS", "OFFHOOK");
				phoneRinging = false;
				break;
			case TelephonyManager.CALL_STATE_RINGING:
				// myAM.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
				// myAM.setStreamVolume(AudioManager.STREAM_RING, 1, 0);
				Log.d("MLS", "RINGING: " + incomingNumber);
				Log.d("MLS",
						"It is: " + myCI.hmNumberToName.get(incomingNumber));
				Log.d("MLS",
						"Should use profile "
								+ myCI.hmNumberToProfile.get(incomingNumber));
				phoneRinging = true;

				break;
			}
		} else { // were not active right now ..
			Log.d("MLS", "Callstate changed, but MLS has not been activated");
		}
	}

	public MyPhoneStateListener(Context inContext, AudioManager AM,
			TelephonyManager TM) {
		myContext = inContext;
		myAM = AM;
		myTM = TM;
		mySettings = new Settings();
		LoadAndSave myLoadAndSave = new LoadAndSave();
		myCI = myLoadAndSave.deserializeCI();
	}

}
